package com.vaadin.thomas.contenttool.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.thomas.contenttool.data.Localization;
import com.vaadin.thomas.contenttool.data.Value;
import com.vaadin.thomas.contenttool.ui.events.PreviewCommittedEvent;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Reindeer;

public class CommitWindow extends Window {

	private static final long serialVersionUID = -6372668549034147553L;
	private Panel itemPanel;
	private TextArea msgField;
	private Label requiredFieldsError;

	public CommitWindow() {

		setCaption(AdminMainLayout.ct().get("contenttool.commit.caption"));
		addStyleName("commitwindow");

		setModal(true);
		center();
		setWidth(50, UNITS_PERCENTAGE);
		setHeight(70, UNITS_PERCENTAGE);
		setIcon(Util.getResource("icons/database_go"));

		VerticalLayout panelContent = new VerticalLayout();
		panelContent.setSizeFull();
		panelContent.setMargin(true);
		panelContent.setSpacing(true);
		setContent(panelContent);

		itemPanel = new Panel();
		itemPanel.addStyleName(Reindeer.PANEL_LIGHT);
		itemPanel.setSizeFull();
		GridLayout itemContent = new GridLayout(2, 1);
		itemContent.setMargin(false);
		itemContent.setColumnExpandRatio(0, 1);
		itemContent.setWidth(100, UNITS_PERCENTAGE);
		itemPanel.setContent(itemContent);

		List<Value> values = new ArrayList<Value>(AdminMainLayout.ct().getModel()
				.getPreview().getValuesInThis().values());
		if (values.isEmpty()) {

			Label emptyLabel = new Label(AdminMainLayout.ct().get(
					"contenttool.commit.noitems"));
			itemPanel.addComponent(emptyLabel);
		} else {
			Collections.sort(values);

			for (Value v : values) {
				CheckBox b = new CheckBox(v.getKey());
				b.setValue(true);
				b.setData(v);
				itemPanel.addComponent(b);

				HorizontalLayout hl = new HorizontalLayout();
				hl.setSpacing(true);
				hl.setMargin(false, true, false, false);
				itemPanel.addComponent(hl);

				if (v.isDeleted()) {
					Embedded e = new Embedded(null, new ThemeResource(
							"icons/cross.png"));
					hl.addComponent(e);
				} else {

					List<String> languages = new ArrayList<String>();
					for (List<Localization> list : v.getLocalizations()
							.values()) {
						for (Localization l : list) {
							languages.add(l.getLanguage().getLanguage());
						}
					}
					for (String lang : languages) {
						Embedded e = Util.getFlag(lang);
						hl.addComponent(e);
					}
				}
			}
		}
		addComponent(itemPanel);
		((AbstractOrderedLayout) getContent()).setExpandRatio(itemPanel, 1);

		msgField = new TextArea(AdminMainLayout.ct().get(
				"contenttool.commit.committer"));
		msgField.setWidth(100, UNITS_PERCENTAGE);
		msgField.setRows(4);
		msgField.setRequired(true);
		msgField.setVisible(!values.isEmpty());
		msgField.setImmediate(true);
		msgField.addListener(new TextChangeListener() {

			private static final long serialVersionUID = -2857609348484555076L;

			@Override
			public void textChange(TextChangeEvent event) {
				if (requiredFieldsError.isVisible()) {
					requiredFieldsError.setVisible(false);
				}
			}
		});
		addComponent(msgField);

		requiredFieldsError = new Label(AdminMainLayout.ct().get(
				"contenttool.commit.fillfields"));
		requiredFieldsError.setVisible(false);
		requiredFieldsError.addStyleName("error");
		addComponent(requiredFieldsError);

		HorizontalLayout hl = new HorizontalLayout();
		hl.setWidth(100, UNITS_PERCENTAGE);
		hl.setSpacing(true);

		Button commit = new Button(AdminMainLayout.ct().get(
				"contenttool.commit.commit"));
		commit.setWidth(100, UNITS_PERCENTAGE);
		commit.addListener(new CommitListener());
		commit.setEnabled(!values.isEmpty());
		hl.addComponent(commit);

		Button cancel = new Button(AdminMainLayout.ct().get(
				"contenttool.commit.cancel"));
		cancel.setWidth(100, UNITS_PERCENTAGE);
		cancel.addListener(new ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				close();
			}
		});
		hl.addComponent(cancel);
		addComponent(hl);
	}

	private class CommitListener implements ClickListener {

		private static final long serialVersionUID = 8400591559754446365L;

		@Override
		public void buttonClick(ClickEvent event) {

			if (msgField.getValue() == null || msgField.getValue().equals("")) {
				requiredFieldsError.setVisible(true);
				return;
			}

			Collection<Value> toStore = new HashSet<Value>();
			Iterator<Component> iter = itemPanel.getContent()
					.getComponentIterator();
			while (iter.hasNext()) {
				Component c = iter.next();
				if (c instanceof CheckBox) {
					if (((CheckBox) c).booleanValue()) {
						toStore.add((Value) ((CheckBox) c).getData());
					}
				}
			}

			// commit
			AdminMainLayout
					.ct()
					.getModel()
					.commitPreview(true, toStore,
							AdminMainLayout.ct().conf().getUserIdToStore(),
							(String) msgField.getValue());
			// set new version for CT
			AdminMainLayout.ct().setPreviewMode(false);
			// notify rest of UI
			AdminMainLayout.bb().fire(new PreviewCommittedEvent());
			close();

		}
	}
}
